<?php
class CommonController
{
    public $startTime;

    function __construct()
    {
        $this->reqLog();
        $this->startTime = microtime(true) * 1000;
    }

    public function _response($data = array()){
        header('Content-type: application/json');

        if($data['errNo'] == "0"){
            $data['errNo'] = "0";
            $data['errMsg'] = "success";
        }else{
            if(empty($data['errMsg'])){
                $data['errMsg'] = error_code($data['errNo']);
            }
        }

        $res = json_encode($data);

        $len = json_encode(strlen($res));

        $endTime = microtime(true) * 1000;
        $runTime =  number_format($endTime - $this->startTime, 3);

        $log = sprintf("res : %s len : %d [runtime = " . $runTime . " ms]", $res, $len, $runTime);

        DLOG($log , "run", "delivery_api");
        echo $res;exit;
    }

    /*
     * 记录请求日志
     *
     * */
    public function reqLog()
    {
        $method = $_SERVER['REQUEST_METHOD'];

        $params = json_encode($_POST);
        $len = strlen($params);

        $log = sprintf("[%s] params : %s len : %d", $method, $params, $len);
        DLOG($log, "run", "delivery_api");

    }

    /*
        错误代码
    */
    public function errNo($code = '')
    {
        $res = array(
            "0" => "正常",
            "-1" => "缺少必要参数",
            "-3" => "系统错误",

            "-40001" => "订单信息不存在",

            "-50001" => "暂无承运人",
            
            "-60001" => "创建运单失败",
        );

        return $res;
    }
}
